//delay=prompt("设置读取间隔(ms)",2000);
delay=2000;
n=100;
song_list=document.getElementById("song_list");
down_div=new Array(n);
lrc_div=new Array(n);
link_head="http://www.google.cn/music/top100/musicdownload?id=";
lrc_head="http://www.google.cn/music/top100/lyrics?id=";
down=new Array(n);
lrc=new Array(n);

function display(i){
   song_title=down_div[i].getElementsByTagName("td")[9].innerHTML;
   song_artist=down_div[i].getElementsByTagName("td")[10].innerHTML;
   song_size=down_div[i].getElementsByTagName("td")[11].innerHTML;
   song_format=down_div[i].getElementsByTagName("td")[12].innerHTML;
   ins_pos=song_list.rows[i].lastChild;
   td_element=document.createElement("td");
   ins_pos.parentNode.insertBefore(td_element,ins_pos.nextSibling);
   down_link=down_div[i].innerHTML.match("http.*\.mp3");
   down_div[i].innerHTML="";//temp solution
   lrc_link=lrc_div[i].innerHTML.match("http.*\.lrc");
   fileName=song_artist+'-'+song_title;
   if(down_link)td_element.innerHTML="<span><small><a href=\""+unescape(down_link)+"\">"+fileName+".mp3</a></small></span>";
   if(td_element.innerHTML)td_element.innerHTML+="<br />";
   if(lrc_link)td_element.innerHTML+="<span><small><a href=\""+unescape(lrc_link)+"\">"+fileName+".lrc</a></small></span>";
}

function get(i){

   function ins_div(){
      ins_pos=document.body.lastChild;
      div=document.createElement("div");
      div.style.display="none";
      ins_pos.parentNode.insertBefore(div,ins_pos.nextSibling);
      return div;
   };

   function get_page(x,div,link){
      x=new XMLHttpRequest();
      x.onreadystatechange=function(){
         if(x.readyState==4){
            div.innerHTML=x.responseText;
         };
      };
      x.open("GET",link,true);
      x.send(null);
   };

   down_div[i]=ins_div();
   lrc_div[i]=ins_div();
   song_id=song_list.rows[i].id.slice(3,255);
   downpage_link=link_head+song_id;
   lrcpage_link=lrc_head+song_id;
   get_page(down[i],down_div[i],downpage_link);
   get_page(lrc[i],lrc_div[i],lrcpage_link);
   setTimeout("display("+i+")",delay);
   j=i+1;
   if(typeof(song_list.rows[i])!="undefined")setTimeout("get("+j+")",delay)
};

get(0);

// vim: set sw=3 ts=3 sts=3 et:
